<template>
  <div>
    <FormTable :formData="formData"
               :columnData="columnData"
               :editData="editData"
               :url="$api.banner_getlist"
               :new_url="this.$api.banner_insert"
               :detail_url="this.$api.banner_info"
               :edit_url="this.$api.banner_save"
               :is_default_edit="true"
               :pageConfig="pageConfig"></FormTable>
  </div>
</template>

<script>
export default {
  data () {
    return {
      formData: [
        {
          name: "name",
          label: "轮播图名称",
          value: "",
          type: "input",
        },
        {
          name: "type",
          label: "位置类型",
          value: '',
          type: "select",
          groups: [
            {
              label: '所有',
              value: ''
            },
          ]
        },
        {
          name: "jump_type",
          label: "地址类型",
          value: '',
          type: "select",
          groups: [
            {
              label: '所有',
              value: ''
            },
          ]
        },

      ],
      columnData: [
        {
          prop: "banner_id",
          label: "id"
        },
        {
          prop: "name",
          label: "名称"
        },
        {
          prop: "banner_url",
          label: "轮播图图片",
          custom: true,
          type: 'image',
          width: '300px',
          height: '100px'
        },
        {
          prop: "jump_type_name",
          label: "地址类型"
        },
        {
          prop: "type_name",
          label: "位置类型"
        },
        {
          prop: "display",
          label: "是否显示",
          custom: true,
          type: 'switch',
          url: this.$api.banner_status
        },
        {
          prop: "sort",
          label: "排序",
          custom: true,
          type: 'input',
          url: this.$api.banner_sort
        },
        {
          prop: 'btn_group',
          label: '操作',
          custom: true,
          type: 'btn_group',
          width: '200px',
          group: [
            {
              name: '编辑',
              type: 'edit',
              color: 'primary',
              detail_mark: 'banner_id',
            },
            {
              name: '删除',
              type: 'delete',
              color: 'danger',
              url: this.$api.banner_status
            }
          ]
        }
      ],
      editData: [
        {
          name: 'banner_id',
          label: '	轮播图id',
          value: '',
          type: 'input',
          display: 'none'
        },
        {
          name: 'name',
          label: '名称',
          value: '',
          type: 'input',
          rules: [
            { required: true, message: "请输入轮播图名称", trigger: "blur" },
          ]
        },
        {
          name: 'web_url',
          label: '跳转链接',
          value: '',
          type: 'input',
        },
        {
          name: 'sort',
          label: '排序',
          value: '',
          type: 'input',
        },
         {
          name: "jump_type",
          label: "位置类型",
          value: '',
          type: "select",
          groups: [],
          rules: [
            { required: true, message: "请选择位置类型", trigger: "blur" },
          ]
        },
        {
          name: "type",
          label: "地址类型",
          value: '',
          type: "select",
          groups: [],
          rules: [
            { required: true, message: "请选择地址类型", trigger: "blur" },
          ]
        },
        {
          name: "display",
          label: "是否显示",
          value: '',
          type: 'switch',
          config: {
            'active_value': '1',
            'inactive-value': '0'
          }
        },

        {
          name: 'banner_url',
          label: '轮播图图片',
          value: '',
          type: 'image_upload',
          width: '100%',
          config: {
            limit: 1,
            accept: ".jpg,.png,.bmp,.gif,.svg",
            fileList: []
            // is_icon:true
          },
          rules: [
            { required: true, message: "请添加轮播图图片", trigger: "blur" },
          ]
        },
      ],
      pageConfig: {
        page: 1,
        pagesize: 10,
      }
    };
  },
  created () {
    this.getType();
  },
  methods: {
    getType () {
      // 获取轮播图地址类型
      let req1 = this.$req.get(this.$api.banner_jumpType)
      // 获取轮播图位置类型
      let req2 = this.$req.get(this.$api.banner_type)
      Promise.all([req1, req2]).then(values => {
        this.dataHandler(values[1].data, 1, 5, 'type_name', 'type_id');
        this.dataHandler(values[0].data, 2, 4, 'jump_type_name', 'jump_type_id');
        console.log(this.editData)
      })
    },
    dataHandler (data, idx1, idx2, labelKey, valueKey) {
      for (var i in data) {
        if (typeof idx1 == 'number') {
          this.formData[idx1].groups.push({
            label: labelKey ? data[i][labelKey] : data[i],
            value: valueKey ? data[i][valueKey] : data[i]
          })
        }
        if (typeof idx2 == 'number') {
          this.editData[idx2].groups.push({
            label: labelKey ? data[i][labelKey] : data[i],
            value: valueKey ? data[i][valueKey] : data[i],
          })
        }
      }
    },
    handleSizeChange (e) {
      console.log(e);
    },
    handleCurrentChange (e) {
      console.log(e);
    },
    submit (e) {
      console.log(e);
    },
    reset (e) {
      console.log(e);
    }
  }
}
</script>

<style lang="scss" scoped>
</style>